import { defineStore } from "pinia";
import { ref } from "vue";
import { ChartAPI, type HarmonicDataItem, type VectorDataItem, type WaveDataItem, type ChartDataItem, ChartType } from "@/api/hmi/chart.api";

/**
 * 图表数据Store
 */
export const useChartStore = defineStore("chart", () => {
  // 响应式状态
  const harmonicData = ref<HarmonicDataItem[]>([]);
  const vectorData = ref<VectorDataItem[]>([]);
  const waveData = ref<WaveDataItem[]>([]);
  const isLoading = ref<boolean>(false);
  const error = ref<string | null>(null);

  /**
   * 获取谐波数据
   * @param cpu CPU编号
   */
  const fetchHarmonicData = (cpu: number): Promise<HarmonicDataItem[]> => {
    return new Promise(async (resolve, reject) => {
      try {
        isLoading.value = true;
        error.value = null;
        
        const response = await ChartAPI.fetchHarmonicData(cpu);
        harmonicData.value = response;
        resolve(response);
      } catch (err) {
        const errorMessage = err instanceof Error ? err.message : "获取谐波数据失败";
        error.value = errorMessage;
        console.error("获取谐波数据失败:", err);
        reject(new Error(errorMessage));
      } finally {
        isLoading.value = false;
      }
    });
  };

  /**
   * 获取矢量数据
   * @param cpu CPU编号
   */
  const fetchVectorData = (cpu: number): Promise<VectorDataItem[]> => {
    return new Promise(async (resolve, reject) => {
      try {
        isLoading.value = true;
        error.value = null;
        
        const response = await ChartAPI.fetchVectorData(cpu);
        vectorData.value = response;
        resolve(response);
      } catch (err) {
        const errorMessage = err instanceof Error ? err.message : "获取矢量数据失败";
        error.value = errorMessage;
        console.error("获取矢量数据失败:", err);
        reject(new Error(errorMessage));
      } finally {
        isLoading.value = false;
      }
    });
  };

  /**
   * 获取波形数据
   * @param cpu CPU编号
   */
  const fetchWaveData = (cpu: number): Promise<WaveDataItem[]> => {
    return new Promise(async (resolve, reject) => {
      try {
        isLoading.value = true;
        error.value = null;
        
        const response = await ChartAPI.fetchWaveData(cpu);
        waveData.value = response;
        resolve(response);
      } catch (err) {
        const errorMessage = err instanceof Error ? err.message : "获取波形数据失败";
        error.value = errorMessage;
        console.error("获取波形数据失败:", err);
        reject(new Error(errorMessage));
      } finally {
        isLoading.value = false;
      }
    });
  };

  /**
   * 根据图表类型获取数据
   * @param type 图表类型
   * @param cpu CPU编号
   */
  const fetchChartData = (type: ChartType, cpu: number): Promise<ChartDataItem[]> => {
    switch (type) {
      case ChartType.HARMONIC:
        return fetchHarmonicData(cpu);
      case ChartType.VECTOR:
        return fetchVectorData(cpu);
      case ChartType.WAVE:
        return fetchWaveData(cpu);
      default:
        return Promise.reject(new Error(`不支持的图表类型: ${type}`));
    }
  };

  /**
   * 获取当前数据
   * @param type 图表类型
   */
  const getCurrentData = (type: ChartType): ChartDataItem[] => {
    switch (type) {
      case ChartType.HARMONIC:
        return harmonicData.value;
      case ChartType.VECTOR:
        return vectorData.value;
      case ChartType.WAVE:
        return waveData.value;
      default:
        return [];
    }
  };

  /**
   * 清空数据
   */
  const clearData = () => {
    harmonicData.value = [];
    vectorData.value = [];
    waveData.value = [];
    error.value = null;
  };

  /**
   * 重置状态
   */
  const resetState = () => {
    clearData();
    isLoading.value = false;
  };

  return {
    // 状态
    harmonicData,
    vectorData,
    waveData,
    isLoading,
    error,

    // 方法
    fetchHarmonicData,
    fetchVectorData,
    fetchWaveData,
    fetchChartData,
    getCurrentData,
    clearData,
    resetState,
  };
});

// 导出hook
export const useChartStoreHook = () => {
  return useChartStore();
};
